#include "subcurve.h"

SubCurve::SubCurve(QWidget *parent) : QCustomPlot(parent)
{

}
SubCurve::~SubCurve()
{

}

void SubCurve::drawCursor(QPainter *painter)
{
    painter->save();

    painter->setRenderHints(QPainter::Antialiasing);
    QRadialGradient radialGradient;
    radialGradient.setCenter(m_Tracking.m_PointCenter);
    radialGradient.setRadius(m_Tracking.m_radius);
    radialGradient.setFocalPoint(m_Tracking.m_PointCenter);
    if(m_Tracking.m_IsMove){
        radialGradient.setColorAt(0.0,QColor(161,140,209,180));
        radialGradient.setColorAt(1.0,QColor(251,194,235,180));
        painter->setPen(QColor(251,194,235));
    }else{
        radialGradient.setColorAt(0.0,QColor(134,143,150,150));
        radialGradient.setColorAt(1.0,QColor(59,97,100,150));
        painter->setPen(QColor(59,97,100));
    }
    painter->setBrush(radialGradient);
    int radius=m_Tracking.m_radius;
    if(m_Tracking.m_PointCenter.rx()<0){ m_Tracking.m_PointCenter.rx()=0; }
    if(m_Tracking.m_PointCenter.rx()>width()){ m_Tracking.m_PointCenter.rx()=width(); }
    if(m_Tracking.m_PointCenter.ry()<radius){ m_Tracking.m_PointCenter.ry()=radius; }
    if(m_Tracking.m_PointCenter.ry()>(height()-radius)){ m_Tracking.m_PointCenter.ry()=(height()-radius); }

    painter->save();
    painter->setPen(QColor(255,255,255));
    painter->drawLine(m_Tracking.m_PointCenter.x(),0,m_Tracking.m_PointCenter.x(),height());
    painter->drawLine(0,m_Tracking.m_PointCenter.y(),width(),m_Tracking.m_PointCenter.y());
    painter->restore();

    painter->drawEllipse(m_Tracking.m_PointCenter,radius,radius);
    painter->restore();

    calculateTrackValue(m_Tracking.m_PointCenter);
}

void SubCurve::paintEvent(QPaintEvent *event)
{
    QCustomPlot::paintEvent(event);
    QPainter painter(this);
    drawCursor(&painter);
}

void SubCurve::mousePressEvent(QMouseEvent *event)
{
    QCustomPlot::mousePressEvent(event);
    m_ClickedPoint=event->pos();
    //判断鼠标是否在焦点，在的话就移动
    bool TmpMoveCursor=m_Tracking.getIsRange(event->pos());
    if(m_Tracking.m_IsMove!=TmpMoveCursor)
    {
        m_Tracking.m_IsMove=TmpMoveCursor;
        m_Tracking.m_First=true;
        update();
    }
}

void SubCurve::mouseReleaseEvent(QMouseEvent *event)
{
    if(m_Tracking.m_IsMove)
    {
        m_Tracking.m_IsMove=false;
        m_Tracking.m_First=false;
        update();
    }
    QCustomPlot::mouseReleaseEvent(event);
}

void SubCurve::mouseMoveEvent(QMouseEvent *event)
{
    if(m_Tracking.m_IsMove)
    {
        m_Tracking.m_PointCenter=event->pos();
        emit update();
    }else{
        QCustomPlot::mouseMoveEvent(event);
    }
}

void SubCurve::showEvent(QShowEvent *event)
{
    if(!m_Tracking.m_IsInit)
    {
        m_Tracking.m_IsInit=true;
        m_Tracking.m_PointCenter=QPoint(width()/2,height()/2);
    }
    QCustomPlot::showEvent(event);
}

void SubCurve::calculateTrackValue(QPoint Point)
{
    if(this->graphCount()==0){ return; }
    double KeyX = this->xAxis->pixelToCoord(Point.x());

    m_CoorPoints.clear();
    QSharedPointer<QCPGraphDataContainer> tmpContainer;
    for(int i=0;i<this->graphCount();i++)
    {
        _COORDINATEVALUE CoorValue;
        tmpContainer = this->graph(i)->data();
        if(KeyX >= tmpContainer->constBegin()->mainKey() && KeyX <= (tmpContainer->constEnd()-1)->mainKey())
        {
            int low = 0, high = tmpContainer->size();
            while(high > low)
            {
                int middle = (low + high) / 2;
                if(KeyX == (tmpContainer->constBegin() + middle)->mainKey())
                {
                    CoorValue.Value = (tmpContainer->constBegin() + middle)->mainValue();
                    CoorValue.Key=(tmpContainer->constBegin()+middle)->mainKey();
                    break;
                }
                if(KeyX > (tmpContainer->constBegin() + middle)->mainKey())
                {
                    low = middle;
                }
                if(KeyX < (tmpContainer->constBegin() + middle)->mainKey())
                {
                    high = middle;
                }
                if(high - low <= 1)
                {
                    CoorValue.Value=(tmpContainer->constBegin()+low)->mainValue();
                    CoorValue.Key=(tmpContainer->constBegin()+low)->mainKey();
                    break;
                }
            }
        }else{
            CoorValue.Value=-25555;
            CoorValue.Key=-25555;
        }
        m_CoorPoints.append(CoorValue);
    }
    emit sig_UpdateTrack();

}
